#include "stack.h"

void  StackInit(Stack* ps)
{
    assert(ps);
    ps->array = nullptr;
    ps->capacity = ps->top = 0;
}

void  StackDestory(Stack* ps)
{
    assert(ps);
    free(ps->array);
    ps->array = nullptr;
    ps->top = ps->capacity = 0;
}

void  StackPush(Stack* ps, STDataType val)
{
    assert(ps);
    if (ps->top == ps->capacity)
    {
        int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        STDataType* newArray = (STDataType*)realloc(ps->array, newCapacity * sizeof(STDataType));
        assert(newArray);
        ps->array = newArray;
        ps->capacity = newCapacity;
    }

    ps->array[ps->top++] = val;
}
void  StackPop(Stack* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    ps->top--;
}

bool  StackEmpty(Stack* ps)
{
    assert(ps);
    return ps->capacity == 0;
}

int  StackSize(Stack* ps)
{
    assert(ps);
    return ps->top;
}

STDataType  StackTop(Stack* ps)
{
    assert(ps);
    return ps->array[ps->top - 1];
}